package me.pinion.service;

import com.jfinal.core.Controller;
import me.pinion.Log;
import me.pinion.kit.CommonKit;
import me.pinion.model.User;


public class AccountService extends Service{



    public User loginByEmail(String email, String password){
        Log.i(CommonKit.encrypt(password));
        return User.dao.findFirst("SELECT * FROM user WHERE email=? AND password=?", email, CommonKit.encrypt(password));
    }

    public User loginByUsername(String username, String password){
        return User.dao.findFirst("SELECT * FROM user WHERE username=? AND password=?", username, CommonKit.encrypt(password));
    }

    public boolean login(Controller controller, String account, String password){
        User user;
        user = this.loginByEmail(account, password);
        if (user != null){
            controller.setSessionAttr("user", User.dao.findById(user.getId()));
            return true;
        }
        user = this.loginByUsername(account, password);
        if (user != null){
            controller.setSessionAttr("user", User.dao.findById(user.getId()));
            return true;
        }
        return false;
    }

    public User register(Controller controller){
        User user = registerByUsername(controller);
        if (user == null){
            user = registerByNumber(controller);
        }
        if (user == null){
            user = registerByEmail(controller);
        }
        if (user != null){
            user.addRole("USER");
            controller.setSessionAttr("user", user);
        }
        return user;
    }

    public User registerByNumber(Controller controller){
        String number = controller.getPara("number");
        if (number == null) return null;
        User user = new User();
        user.set("username", "User_" + number)
            .set("number", number)
            .setPassword(controller.getPara("password"));
        user.save();
        return user;
    }

    public User registerByEmail(Controller controller){
        String email = controller.getPara("email");
        if (email == null) return null;
        User user = new User();
        user.set("username", email.substring(0, email.indexOf("@")))
            .set("email", email)
            .setPassword(controller.getPara("password"));
        user.save();
        return user;
    }

    public User registerByUsername(Controller controller){
        String username = controller.getPara("username");
        if(username == null) return null;
        User user = new User();
        user.set("username", username).setPassword(controller.getPara("password"));
        user.save();
        return user;
    }

    public boolean exist(Controller c){
        User user = null;
        if (user == null && c.getPara("username") != null){
            user = User.dao.findByUsername(c.getPara("username"));
        }

        if (user == null && c.getPara("email") != null){
            user = User.dao.findByEmail(c.getPara("email"));
        }

        if (user == null && c.getPara("number") != null){
            user = User.dao.findByEmail(c.getPara("number"));
        }
        return user != null;
    }
}
